package shuashuami.hb.com.fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import shuashuami.hb.com.avtivity.BigImagesActivity;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class EvaluationDescriptionFragment extends Fragment {

    private TextView tvComment;
    private TextView tvCopy;

    private LinearLayout llImages;

    private GlidUtil glidUtil;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_evaluation_description, container, false);
        initView(view);
        initData();
        initClick();
        return view;
    }

    private void initView(View view) {
        tvComment = (TextView) view.findViewById(R.id.tv_evaluation_desription_comment);
        tvCopy = (TextView) view.findViewById(R.id.tv_copy_comment);
        llImages = (LinearLayout) view.findViewById(R.id.ll_evaluation_desription_comment_image);

    }

    private void initData() {
        glidUtil = new GlidUtil(getActivity());
    }

    private void initClick() {
        tvCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cmb = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(tvComment.getText().toString().trim()); //将内容放入粘贴管理器,在别的地方长按选择"粘贴"即可
                ToastUtil.showShort(getActivity(), "复制文本成功");
            }
        });
    }

    public void setMessage(String comment, List<String> images) {
        tvComment.setText(comment);
        setImages(images);
    }

    private void setImages(final List<String> images) {
        final shuashuami.hb.com.Application application = (shuashuami.hb.com.Application) getActivity().getApplication();
        application.getImages().clear();
        for (int i = 0; i < images.size() && i < 3; i++) {
            final String urls = images.get(i);
            final ImageView imageView = new ImageView(getActivity());
            imageView.setLayoutParams(new LinearLayout.LayoutParams(250, 250));  //设置图片宽高
            imageView.setImageResource(R.mipmap.ic_launcher);
            glidUtil.uploadRound(urls, 10, imageView);
            llImages.addView(imageView); //动态添加图片
            final int j = i;
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    application.setImages(images);
                    Intent intent = new Intent(getActivity(), BigImagesActivity.class);
                    startActivity(intent);
                }
            });
            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    saveCroppedImage(((GlideBitmapDrawable) imageView.getDrawable()).getBitmap());
                    return true;
                }
            });

        }
    }

    //保存图片
    private void saveCroppedImage(Bitmap bmp) {
        File file = new File("/sdcard/myFolder");
        if (!file.exists())
            file.mkdir();

        Date date = new Date();
        file = new File("/sdcard/" + date.getTime() + ".jpg".trim());
        String fileName = file.getName();
        String mName = fileName.substring(0, fileName.lastIndexOf("."));
        String sName = fileName.substring(fileName.lastIndexOf("."));

        // /sdcard/myFolder/temp_cropped.jpg
        String newFilePath = "/sdcard/myFolder" + "/" + mName + sName;
        file = new File(newFilePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
            ToastUtil.showShort(getContext(), "保存成功,保存路径为" + newFilePath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
